﻿using System;
using System.Text;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Constants;
using MonkeyWarranty.Web.Common;
using Resources;

namespace MonkeyWarranty.Web.Reports.InstantTicketReports
{
    public partial class EmployeeReportExport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.Admin))
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            var dtFrom = Request.Params["dtFrom"] ?? string.Empty;
            var dtTo = Request.Params["dtTo"] ?? string.Empty;

            var html = GenerateHtml(dtFrom, dtTo);
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
                string.Format("attachment;filename=TongHopCongViec_{0}_{1}.xls", dtFrom.Replace('/', '-'), dtTo.Replace('/', '-')));
            Response.ContentEncoding = Encoding.UTF8;
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(Encoding.Unicode.GetPreamble());
            var bytes = Encoding.Unicode.GetBytes(html);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        private static string GenerateHtml(string dtFrom, string dtTo)
        {
            var results = ReportingBiz.ReportEmployeeInstantTicket(dtFrom, dtTo, GlobalStrings.DateFormat);

            var headers = new[] { "Nhân viên", "Số lượng công việc", "Khoảng cách (km)", "Số giờ làm việc" };

            var sbHtml = new StringBuilder();
            sbHtml.Append("<html><body style='font-size:12px !important;font-family:Times New Roman !important;'><head><meta charset=\"utf-8\" />");
            sbHtml.Append("<style type='text/css'>td {vertical-align:top;}</style>");
            sbHtml.Append("</head><body>");
            sbHtml.Append("<table cellpadding='0' cellspacing='0' style='border-collapse:collapse;'>");

            // headers
            sbHtml.Append("<tr>");
            foreach (var header in headers)
            {
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'><strong>{0}</strong></td>", header);
            }
            sbHtml.Append("</tr>");

            //const string formatText = "<td style='border:solid 0.1pt;'>{0}</td>";
            const string formatTextLeft = "<td style='border:solid 0.1pt; text-align:left;'>{0}</td>";
            const string formatTextRight = "<td style='border:solid 0.1pt; text-align:right;'>{0}</td>";
            //const string formatDate = "<td style='border:solid 0.1pt; text-align:right;'>{0}</td>";

            foreach (var result in results)
            {
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat(formatTextLeft, result.EmployeeName);
                sbHtml.AppendFormat(formatTextRight, result.NumOfWorks);
                sbHtml.AppendFormat(formatTextRight, result.TotalDistance);
                sbHtml.AppendFormat(formatTextRight, result.TotalHoursString);
                sbHtml.Append("</tr>");
            }

            sbHtml.Append("</table>");
            sbHtml.Append("</body></html>");

            return sbHtml.ToString();
        }
    }
}